<?php
/*
 * @Author: your name
 * @Date: 2021-03-15 10:17:45
 * @LastEditTime: 2021-05-12 14:01:59
 * @LastEditors: Please set LastEditors
 * @Description: HCM->宿舍管理系统对接control类
 * @FilePath: \dorm_manage_DEV\application\controllers\Hcm\Hcm.php
 */
// 指定允许其他域名访问  
header('Access-Control-Allow-Origin:*');
// 响应类型  
header('Access-Control-Allow-Methods:POST');
// 响应头设置  
header('Access-Control-Allow-Headers:x-requested-with,content-type:application/json,charset:utf-8');
require_once 'PHPExcel-1.8\Classes\PHPExcel.php';
require_once 'PHPExcel-1.8\Classes\PHPExcel\IOFactory.php';
// require_once 'PHPExcel\Classes\PHPExcel\Reader\Excel5.php';
require_once 'PHPExcel-1.8\Classes\PHPExcel\Reader\Excel2007.php';

// HCM->宿舍管理系统对接control类
class Hcm extends CI_Controller
{   
    // 构造方法
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Hcm/Hcm_manage');
        $this->load->helper('url_helper');
        $this->load->library('session');
        $this->load->helper('file');
        $this->load->helper(array('form', 'url', 'date'));  
    }

    /**
     * @description: HCM员工部门岗位变更同步宿舍管理系统
     * @param {*}
     * @return {*}
     */
    public function Dorm_hcm()
    {
        //入住信息写入日志
        write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
        write_file('logs/logs_dormHcmUpdate.txt', date('Y-m-d H:i:s'), 'a');
        write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
        write_file('logs/logs_dormHcmUpdate.txt', '开始同步', 'a');

        $arr = [];
        //正式
        $soapURL = 'http://DGS4PRD01.degson.com:8000/sap/bc/srt/wsdl/flv_10002A111AD1/bndg_url/sap/bc/srt/rfc/sap/zhr_i_dorm_liveinformationws/800/zhr_i_dorm_liveinformationws/zhr_i_dorm_liveinformationws?sap-client=800'; 
        $client = new SoapClient($soapURL, ['login' => 'L20891', 'password' => '882158']);  
        $IT_TAB = new stdClass();   
        $IT_TAB1 = new stdClass();
        $IT_TAB1->IT_TAB = $IT_TAB;
        $result = $client->ZHR_O_DORM_INFORMATION($IT_TAB1);
        $message = $result->E_STATUS->MESSAGE;

        if(empty($result->IT_TAB->item))
        {
            $number = 0;
        }
        else 
        {
            if(is_array($result->IT_TAB->item))
            {
                $number = count($result->IT_TAB->item);
            }
            else
            {
                $number = 1;
            }
        }

        write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
        write_file('logs/logs_dormHcmUpdate.txt', date('Y-m-d H:i:s'), 'a');
        write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
        write_file('logs/logs_dormHcmUpdate.txt', "部门岗位信息:$message 总条数:$number", 'a');

        if($number > 0)
        {
            write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
            write_file('logs/logs_dormHcmUpdate.txt', date('Y-m-d H:i:s'), 'a');
            write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
            write_file('logs/logs_dormHcmUpdate.txt', '-------------开始更新-------------', 'a');
    
            
            write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
            write_file('logs/logs_dormHcmUpdate.txt', date('Y-m-d H:i:s'), 'a');
            write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
            write_file('logs/logs_dormHcmUpdate.txt', '-------------接收返回-------------', 'a');
            //查询成功
            if($result->E_STATUS->TYPE == 'S')
            {
                if(is_array($result->IT_TAB->item))
                {
                    for($i=0; $i<count($result->IT_TAB->item); $i++)
                    {
                        $jobNumber = substr($result->IT_TAB->item[$i]->PERNR, 3, 5);
                        $result_json = $this->Hcm_manage->hcm_update(substr($result->IT_TAB->item[$i]->PERNR, 3, 5), $result->IT_TAB->item[$i]->ZDEPARTMENT, $result->IT_TAB->item[$i]->ZPLANS);
                        array_push($arr, $result_json);
                        $result_json = json_decode($result_json);
                        $returnMessage = $result_json->msg;
        
                        write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
                        write_file('logs/logs_dormHcmUpdate.txt', date('Y-m-d H:i:s'), 'a');
                        write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
                        write_file('logs/logs_dormHcmUpdate.txt', "$jobNumber:$returnMessage", 'a'); 
                    }
                }
                else
                {
                    $jobNumber = substr($result->IT_TAB->item->PERNR, 3, 5);
                    $result_json = $this->Hcm_manage->hcm_update(substr($result->IT_TAB->item->PERNR, 3, 5), $result->IT_TAB->item->ZDEPARTMENT, $result->IT_TAB->item->ZPLANS);
                    $result_json = json_decode($result_json);
                    $returnMessage = $result_json->msg;
                }
                
                write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
                write_file('logs/logs_dormHcmUpdate.txt', date('Y-m-d H:i:s'), 'a');
                write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
                write_file('logs/logs_dormHcmUpdate.txt', "$jobNumber:$returnMessage", 'a'); 
            }
            else
            {
                $result_json = json_encode(array('res' => '40002', 'msg' => $result->E_STATUS->MESSAGE, 'data' => ''));
                $resultMessage = $result->E_STATUS->MESSAGE;
                write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
                write_file('logs/logs_dormHcmUpdate.txt', date('Y-m-d H:i:s'), 'a');
                write_file('logs/logs_dormHcmUpdate.txt', "\r\n", 'a');
                write_file('logs/logs_dormHcmUpdate.txt', "$resultMessage", 'a'); 
            }   
        }
        if(count($arr)>0)
        {
            print_r($arr);
        }
    }
    
    //宿舍管理系统入住日期，退宿日期同步Hcm
    // public function hcm_LiveRetreat()
    // {
    //     // $date = $_POST['date'];

    //     $date = date('Y-m-d');

    //     $result_json = $this->Hcm_manage->hcm_live_retreat($date);

    //     // json key值双引号通过正则表达式去除
    //     $result_json = preg_replace('/"(\w+)"(\s*:\s*)/is', '$1$2', $result_json);
        
    //     echo $result_json;

    //     return 0;    
    // }
    
    /*入住，退宿信息同步
    1.同步入住信息
    2.同步退宿信息
    */

    /**
     * @description:宿舍管理系统住宿信息同步HCM 
     * @param {*}
     * @return {*}
     */
    public function hcm_stayUpdate()
    {
        $arr = [];
        $arrUpdate = [];
        $arr1 = [];
        //入住信息写入日志
        write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
        write_file('logs/logs_stayInformation_update.txt', date('Y-m-d H:i:s'), 'a');
        write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
        write_file('logs/logs_stayInformation_update.txt', '开始同步', 'a');
        
        //取需推送的入住数据
        $live = $this->Hcm_manage->hcm_live();
        $live = json_decode($live);
        $liveMessage = $live->msg;
        if(empty($live->data))
        {
            $liveNumber = 0; 
        }
        else 
        {
            $liveNumber = count($live->data);
            
        }

        write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
        write_file('logs/logs_stayInformation_update.txt', date('Y-m-d H:i:s'), 'a');
        write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
        write_file('logs/logs_stayInformation_update.txt', "入住信息:$liveMessage 总条数:$liveNumber", 'a');

        //取需推送的退宿数据
        $retrea = $this->Hcm_manage->hcm_retrea();
        $retrea = json_decode($retrea);
        $retreaMessage = $retrea->msg;
        if(empty($retrea->data))
        {
            $retreaNumber = 0; 
        }
        else 
        {
            $retreaNumber = count($retrea->data);
        }

        write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
        write_file('logs/logs_stayInformation_update.txt', date('Y-m-d H:i:s'), 'a');
        write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
        write_file('logs/logs_stayInformation_update.txt', "退宿信息:$retreaMessage 总条数:$retreaNumber", 'a');

        if($liveNumber > 0 || $retreaNumber > 0)
        {
            //调用webservice进行住宿信息更新
            
            //正式
            $soapURL = 'http://DGS4PRD01.degson.com:8000/sap/bc/srt/wsdl/flv_10002A101AD1/bndg_url/sap/bc/srt/rfc/sap/zhr_i_dorm_liveinformationws/800/zhr_i_dorm_liveinformationws/zhr_i_dorm_liveinformationws?sap-client=800';
            $client = new SoapClient($soapURL, ['login' => 'L20891', 'password' => '882158']); 

            $T_TAB = new stdClass('T_TAB');
            $IT_TAB_ITEM = new stdClass('IT_TAB_ITEM ');
            $OUT_TAB = new stdClass('OUT_TAB');
            $IT_TAB = [];

            if($live->res == '40000')
            {
                // 入住信息推送
                for($i=0; $i<count($live->data); $i++)
                {
                    //传入结构
                    $IT_TAB_ITEM->PERNR = $live->data[$i]->jobNumber;                   //工号
                    $IT_TAB_ITEM->NACHN = $live->data[$i]->name;                        //姓名
                    $IT_TAB_ITEM->BEGDA = $live->data[$i]->liveDate;                    //入住日期
                    $IT_TAB_ITEM->ZBETRG = '';                                          //退宿日期
                    $IT_TAB_ITEM->ZTYPE = '1';                                          //类型 1.入住 2.退宿
                    $IT_TAB_ITEM->ZREPETITIONCHECK = $live->data[$i]->repetitionCheck;  //是否重复入住
                    array_push($IT_TAB, $IT_TAB_ITEM);
                    $IT_TAB_ITEM = null;
                    $IT_TAB_ITEM = new StdClass('$IT_TAB_ITEM');
                }
            }

            // 退宿信息推送
            if($retrea->res == '40000')
            {
                for($j=0; $j<count($retrea->data); $j++)
                {
                    //传入结构
                    $IT_TAB_ITEM->PERNR = $retrea->data[$j]->jobNumber;     //工号
                    $IT_TAB_ITEM->NACHN = $retrea->data[$j]->name;          //姓名
                    $IT_TAB_ITEM->BEGDA = '';                               //入住日期
                    $IT_TAB_ITEM->ZBETRG = $retrea->data[$j]->retreatDate;  //退宿日期
                    $IT_TAB_ITEM->ZTYPE = '2';                              //类型 1.入住 2.退宿
                    $IT_TAB_ITEM->ZREPETITIONCHECK = $retrea->data[$j]->repetitionCheck;  //是否重复入住
                    array_push($IT_TAB, $IT_TAB_ITEM);
                    $IT_TAB_ITEM = null;
                    $IT_TAB_ITEM = new StdClass('$IT_TAB_ITEM');
                }
            }

            $T_TAB->IT_TAB = $IT_TAB;
            $T_TAB->OUT_TAB = $OUT_TAB;
            write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
            write_file('logs/logs_stayInformation_update.txt', date('Y-m-d H:i:s'), 'a');
            write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
            write_file('logs/logs_stayInformation_update.txt', '-------------开始请求-------------', 'a');
             
            //异常捕获
            try{
                $result = $client->ZHR_I_DORM_LIVEINFORMATION($T_TAB);
            }catch(Exception $e){
                print_r($e->getMessage());
            }

            if(is_array($result->OUT_TAB->item))
            {
                for($x=0; $x<count($result->OUT_TAB->item); $x++)
                {
                    if($result->OUT_TAB->item[$x]->E_TYPE == 'S')
                    {
                        $arr = array(
                            'jobNumber' => substr($result->OUT_TAB->item[$x]->PERNR, 3, 5),
                            'hcmUpdate' => 'X'
                        );
                        array_push($arrUpdate, $arr);
                    }
                }
            }
            else
            {
                if($result->OUT_TAB->item->E_TYPE == 'S')
                {
                    $arr = array(
                        'jobNumber' => substr($result->OUT_TAB->item->PERNR, 3, 5),
                        'hcmUpdate' => 'X'
                    );
                    array_push($arrUpdate, $arr);
                }
            }
            //更新同步标识
            if(count($arrUpdate)>0)
            {
                $this->Hcm_manage->hcmUpdate($arrUpdate);
            }

            write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
            write_file('logs/logs_stayInformation_update.txt', date('Y-m-d H:i:s'), 'a');
            write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
            write_file('logs/logs_stayInformation_update.txt', '-------------接收返回-------------', 'a');

            if (is_array($result->OUT_TAB->item) == TRUE)
            {
                for($j=0 ;$j<count($result->OUT_TAB->item); $j++)
                {
                    $jobNumber = substr($result->OUT_TAB->item[$j]->PERNR, 3 ,5);
                    $name = $result->OUT_TAB->item[$j]->NAME;
                    $message = $result->OUT_TAB->item[$j]->E_MESG;
                    write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
                    write_file('logs/logs_stayInformation_update.txt', date('Y-m-d H:i:s'), 'a');
                    write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
                    write_file('logs/logs_stayInformation_update.txt', "$jobNumber $name:$message", 'a'); 
                }
            }
            else
            {
                $jobNumber = substr($result->OUT_TAB->item->PERNR, 3 ,5);
                $name = $result->OUT_TAB->item->NAME;
                $message = $result->OUT_TAB->item->E_MESG;
                write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
                write_file('logs/logs_stayInformation_update.txt', date('Y-m-d H:i:s'), 'a');
                write_file('logs/logs_stayInformation_update.txt', "\r\n", 'a');
                write_file('logs/logs_stayInformation_update.txt', "$jobNumber $name:$message", 'a'); 
            }
        }
    }

    /**
     * @description: 员工信息HCM导入宿舍管理系统
     * @param {*}
     * @return {*}
     */
    public function  hcm_peopleInformation()
    {
        $arr = [];
        //正式
        $soapURL = 'http://DGS4PRD01.degson.com:8000/sap/bc/srt/wsdl/flv_10002A111AD1/bndg_url/sap/bc/srt/rfc/sap/zhr_o_information_updatews/800/zhr_o_information_updatews/zhr_o_information_updatews?sap-client=800';
        $client = new SoapClient($soapURL, ['login' => 'L20891', 'password' => '882158']);  
        $IT_TAB = new stdClass();   
        $IT_TAB1 = new stdClass();
        $IT_TAB1->IT_TAB = $IT_TAB;
        $result = $client->ZHR_O_INFORMATION_UPDATE($IT_TAB1);

        //查询成功
        if($result->E_STATUS->TYPE == 'S')
        {
            //取系统中所有入住人员的工号
            $query = $this->Hcm_manage->dorm_livePeople();
            $query = json_decode($query);

            //数据更新
            for($i=0; $i<count($result->IT_TAB->item); $i++)
            {
                $result_json = $this->Hcm_manage->hcm_peopleInformation(substr($result->IT_TAB->item[$i]->PERNR, 3, 5), $result->IT_TAB->item[$i]->ICNUM, $result->IT_TAB->item[$i]->ORGEH_1, $result->IT_TAB->item[$i]->ORGEH_2, $result->IT_TAB->item[$i]->ORGEH_3, $result->IT_TAB->item[$i]->STEXT, $result->IT_TAB->item[$i]->USRID, $result->IT_TAB->item[$i]->LOCAT);
                array_push($arr, $result_json);
            }
        }
        else
        {
            $result_json = json_encode(array('res' => '40002', 'msg' => $result->E_STATUS->MESSAGE, 'data' => ''));
            echo $result_json;
        }

        if(count($arr)>0)
        {
            print_r($arr);
        }
    }

    /**
     * @description: 工号更新 宿舍管理系统->HCM
     * @param {*}
     * @return {*}
     */
    public function hcm_jobnumber_update()
    {
        $arr = [];

        write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
        write_file('logs/logs_jobnumber_update.txt', date('Y-m-d H:i:s'), 'a');
        write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
        write_file('logs/logs_jobnumber_update.txt', '开始同步', 'a');

        //需更新工号数据取出
        $jobNmuberUpdate = $this->Hcm_manage->jobnumber_update();
        $jobNmuberUpdate = json_decode($jobNmuberUpdate);
        $jobNmuberUpdateMessage = $jobNmuberUpdate->msg;
        if(empty($jobNmuberUpdate->data))
        {
            $jobNmuberUpdateNumber = 0; 
        }
        else 
        {
            $jobNmuberUpdateNumber = count($jobNmuberUpdate->data);
        }

        write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
        write_file('logs/logs_jobnumber_update.txt', date('Y-m-d H:i:s'), 'a');
        write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
        write_file('logs/logs_jobnumber_update.txt', "$jobNmuberUpdateMessage,总条数:$jobNmuberUpdateNumber", 'a');

        if($jobNmuberUpdateNumber > 0)
        {
            //正式
            $soapURL = 'http://DGS4PRD01.degson.com:8000/sap/bc/srt/wsdl/flv_10002A111AD1/bndg_url/sap/bc/srt/rfc/sap/zhr_o_dorm_pernr_updatews/800/zhr_o_dorm_pernr_updatews/zhr_o_dorm_pernr_updatews?sap-client=800';
            $client = new SoapClient($soapURL, ['login' => 'L20891', 'password' => '882158']);
            $T_TAB = new stdClass('T_TAB');
            $IT_TAB_ITEM = new stdClass('IT_TAB_ITEM ');
            $OUT_TAB = new stdClass('OUT_TAB');
            $IT_TAB = [];
    
            if($jobNmuberUpdate->res = '40000')
            {
                //工号推送数据
                for($i=0; $i<count($jobNmuberUpdate->data); $i++)
                {
                    $IT_TAB_ITEM->ICNUM = $jobNmuberUpdate->data[$i]->idNumber;
                    $IT_TAB_ITEM->PERNR = '';
                    array_push($IT_TAB, $IT_TAB_ITEM);
                    $IT_TAB_ITEM = null;
                    $IT_TAB_ITEM = new StdClass('$IT_TAB_ITEM');
                }
            }
    
            $T_TAB->IT_TAB = $IT_TAB;
            $T_TAB->OUT_TAB = $OUT_TAB;
            write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
            write_file('logs/logs_jobnumber_update.txt', date('Y-m-d H:i:s'), 'a');
            write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
            write_file('logs/logs_jobnumber_update.txt', '-------------开始请求-------------', 'a');
    
            // 调用HCM函数,取得身份证号
            try{
                $result = $client->ZHR_O_DORM_PERNR_UPDATE($T_TAB);
            }catch(Exception $e){
                print_r($e->getMessage());
            }

            write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
            write_file('logs/logs_jobnumber_update.txt', date('Y-m-d H:i:s'), 'a');
            write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
            write_file('logs/logs_jobnumber_update.txt', '-------------接收返回-------------', 'a');

            if (is_array($result->OUT_TAB->item) == TRUE)
            {
                for($j=0; $j<count($result->OUT_TAB->item); $j++)
                {
                    $idNumber = $result->OUT_TAB->item[$j]->ICNUM;
                    // $jobNumber = substr($result->OUT_TAB->item[$j]->PERNR, 3 ,5);
                    $message = $result->OUT_TAB->item[$j]->E_MESG;
                    write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
                    write_file('logs/logs_jobnumber_update.txt', date('Y-m-d H:i:s'), 'a');
                    write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
                    write_file('logs/logs_jobnumber_update.txt', "$idNumber:$message", 'a'); 

                     //将从HCM取到的工号更新到宿舍系统
                    $resultUpdate = $this->Hcm_manage->hcm_jobNumberUpdate($result->OUT_TAB->item[$j]->ICNUM, substr($result->OUT_TAB->item[$j]->PERNR, 3 ,5));
                    $resultUpdate = json_decode($resultUpdate);
                    $jobNumberUpdateMessage = $resultUpdate->msg;
                    $UpdateIdNumber = $result->OUT_TAB->item[$j]->ICNUM;
                    $UpdateJobNumber = substr($result->OUT_TAB->item[$j]->PERNR, 3, 5);

                    write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
                    write_file('logs/logs_jobnumber_update.txt', date('Y-m-d H:i:s'), 'a');
                    write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
                    write_file('logs/logs_jobnumber_update.txt', "身份证：$UpdateIdNumber 工号:$UpdateJobNumber $jobNumberUpdateMessage", 'a');
                }
            }
            else 
            {
                $message = $result->OUT_TAB->item->E_MESG;
                $idNumber = $result->OUT_TAB->item->ICNUM;
                write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
                write_file('logs/logs_jobnumber_update.txt', date('Y-m-d H:i:s'), 'a');
                write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
                write_file('logs/logs_jobnumber_update.txt', "$idNumber:$message", 'a'); 

                 //将从HCM取到的工号更新到宿舍系统
                $resultUpdate = $this->Hcm_manage->hcm_jobNumberUpdate($result->OUT_TAB->item->ICNUM, substr($result->OUT_TAB->item->PERNR, 3 ,5));
                $resultUpdate = json_decode($resultUpdate);
                $jobNumberUpdateMessage = $resultUpdate->msg;
                $UpdateIdNumber = $result->OUT_TAB->item->ICNUM;
                $UpdateJobNumber = substr($result->OUT_TAB->item->PERNR, 3, 5);

                write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
                write_file('logs/logs_jobnumber_update.txt', date('Y-m-d H:i:s'), 'a');
                write_file('logs/logs_jobnumber_update.txt', "\r\n", 'a');
                write_file('logs/logs_jobnumber_update.txt', "身份证：$UpdateIdNumber 工号:$UpdateJobNumber $jobNumberUpdateMessage", 'a');
            }
        }
        return 0;
    }

    /**
     * @description: 根据前端传输的二级组织取HCM的三级组织
     * @param {*}
     * @return {*}
     */
    public function hcm_Tertiary_Departme()
    {
        $secondary_Departme = $_POST['secondary_Departme']; //二级组织
        // $secondary_Departme = '集团信息管控中心';
        $arr = [];

        // 调用SAP接口获取三级组织
        //正式
        $soapURL = 'http://DGS4PRD01.degson.com:8000/sap/bc/srt/wsdl/flv_10002A111AD1/bndg_url/sap/bc/srt/rfc/sap/zhr_o_tertiary_departmews/800/zhr_o_tertiary_departmews/zhr_o_tertiary_departmews?sap-client=800';
        $client = new SoapClient($soapURL, ['login' => 'L20891', 'password' => '882158']); 
        $T_TAB = new stdClass();
        $T_TAB->IT_TAB = [];
        $T_TAB->IT_TAB[0] = new stdClass();
        $T_TAB->IT_TAB[0]->ORGEH_2 = $secondary_Departme;
        $T_TAB->IT_TAB[0]->ORGEH_3 = '';
        $result = $client->ZHR_O_TERTIARY_DEPARTME($T_TAB);
        if($result->E_TYPE == 'S')
        {
            for($i=0; $i<count($result->IT_TAB->item); $i++)
            {
                array_push($arr, $result->IT_TAB->item[$i]->ORGEH_3);
            }
            $result_json = json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $arr)); 
        }
        elseif($result->E_TYPE == 'E')
        {
            $result_json = json_encode(array('res' => '40001', 'msg' => $result->E_MESSAGE, 'data' => ''));
        }
        
        echo $result_json;

        return 0;
    }

        /**
     * @description: 查询HCM中的所有二级组织
     * @param {*}
     * @return {*}
     */
    public function hcm_Secondary_Departme()
    {
        $arr = [];
        //正式
        $soapURL = 'http://DGS4PRD01.degson.com:8000/sap/bc/srt/wsdl/flv_10002A111AD1/bndg_url/sap/bc/srt/rfc/sap/zhr_o_secondary_departmews/800/zhr_o_secondary_departmews/zhr_o_secondary_departmews?sap-client=800';
        $client = new SoapClient($soapURL, ['login' => 'L20891', 'password' => '882158']); 
        $T_TAB = new stdClass();
        $IT_TAB = new stdClass();
        $T_TAB->IT_TAB = $IT_TAB;
        $result = $client->ZHR_O_SECONDARY_DEPARTME($T_TAB);
        if($result->E_TYPE = 'S')
        {
            for($i=0; $i<count($result->IT_TAB->item); $i++)
            {   
                array_push($arr, $result->IT_TAB->item[$i]->ORGEH_2);
            }
            $result_json = json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $arr));
        }
        elseif($result->E_TYPE = 'E')
        {
            $result_json = json_encode(array('res' => '40001', 'msg' => $result->E_MESSAGE, 'data' => ''));
        }

        echo $result_json;

        return 0;
    }

    /**
     * @description: 前端调用花名册接口
     * @param {*}
     * @return {*}
     */
    public function hcm_people_information_post()
    {
        $limit = $_POST['page'];
        
        // $limit = '1';

        $result_json = $this->hcm_people_information($limit);

        echo $result_json;

        return 0;
    }
    

    /**
     * @description: HCM花名册数据存入宿舍管理系统数据库
     * @param {*}
     * @return {*}
     */
    public function hcm_people_information($limit)
    {
        // $limit = 1;
        $arr = [];
        $soapURL = 'http://DGS4PRD01.degson.com:8000/sap/bc/srt/wsdl/flv_10002A111AD1/bndg_url/sap/bc/srt/rfc/sap/zhr_o_employee_informationws/800/zhr_o_employee_informationws/zhr_o_employee_informationws?sap-client=800';
        $client = new SoapClient($soapURL, ['login' => 'L20891', 'password' => '882158']); 
        $T_TAB = new stdClass();
        $OUT_TAB = new stdClass();
        $T_TAB->OUT_TAB = $OUT_TAB;
        $result = $client->ZHR_O_EMPLOYEE_INFORMATION($T_TAB);
        if($result->E_TYPE == 'S')
        {
            for($i=0; $i<count($result->OUT_TAB->item); $i++)
            {
                array_push($arr, $result->OUT_TAB->item[$i]);
            }

            //调用方法将数据存入数据库
            $resultRoster = $this->Hcm_manage->hcmUpdateRoster($arr);
            $resultRoster = json_decode($resultRoster);

            if($resultRoster->res == '40000')
            {   
                //取分页数据
                $resultPagination = $this->Hcm_manage->hcmRosterPagination($limit);
                $resultPagination = json_decode($resultPagination);

                //取全部数据
                $resultPaginationTotal = $this->Hcm_manage->hcmRosterPaginationTotal();
                $resultPaginationTotal = json_decode($resultPaginationTotal);

                //查询成功后清空数据库表
                $this->db->truncate('dorm_roster');
                
                $result_json = json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $resultPagination->data, 'totalPage' => count($resultPaginationTotal->data)));
            }
            else
            {
                $result_json = json_encode(array('res' => '40000', 'msg' => '更新失败', 'data' => ''));
            }
        }
        else
        {
            $result_json = json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
        }

        return $result_json;
    }
            
    /**
     * @description: 人员花名册信息excel导出
     * @param {*}
     * @return {*}
     */    
    public function excel_employee_information()
    {
        $arr = [];
        $soapURL = 'http://DGS4PRD01.degson.com:8000/sap/bc/srt/wsdl/flv_10002A111AD1/bndg_url/sap/bc/srt/rfc/sap/zhr_o_employee_informationws/800/zhr_o_employee_informationws/zhr_o_employee_informationws?sap-client=800';
        $client = new SoapClient($soapURL, ['login' => 'L20891', 'password' => '882158']); 
        $T_TAB = new stdClass();
        $OUT_TAB = new stdClass();
        $T_TAB->OUT_TAB = $OUT_TAB;
        $result = $client->ZHR_O_EMPLOYEE_INFORMATION($T_TAB);
        if($result->E_TYPE == 'S')
        {
            for($i=0; $i<count($result->OUT_TAB->item); $i++)
            {
                array_push($arr, $result->OUT_TAB->item[$i]);
            }
        }

        //调用方法将数据存入数据库
        $resultRoster = $this->Hcm_manage->hcmUpdateRoster($arr);
        
        // 创建一个处理对象实例   
        $objExcel = new \PHPExcel();  
        // 创建文件格式写入对象实例, uncomment   
        $objProps = $objExcel->getProperties();  
        //设置创建者  TODO:
        $objProps->setCreator('admin');
        //描述  
        // $objProps->setDescription("入住信息汇总");  
        $objActSheet = $objExcel->getActiveSheet();
        //单元格赋值    
        $objActSheet->setCellValue('A1','工号');
        $objActSheet->setCellValue('B1','姓名');
        $objActSheet->setCellValue('C1','性别');
        $objActSheet->setCellValue('D1','一级部门名称');
        $objActSheet->setCellValue('E1','二级部门名称');
        $objActSheet->setCellValue('F1','三级部门名称');
        $objActSheet->setCellValue('G1','四级部门名称');
        $objActSheet->setCellValue('H1','五级部门名称');
        $objActSheet->setCellValue('I1','六级部门名称');
        $objActSheet->setCellValue('J1','出生日期');
        $objActSheet->setCellValue('K1','婚姻状况');
        $objActSheet->setCellValue('L1','入职日期');
        $objActSheet->setCellValue('M1','离职日期');
        $objActSheet->setCellValue('N1','工龄');
        $objActSheet->setCellValue('O1','岗位');
        $objActSheet->setCellValue('P1','联系方式');
        $objActSheet->setCellValue('Q1','身份证号');
    
        $num = 2;
        
        $data = $this->Hcm_manage->hcm_people_information_excle();
        $data = json_decode($data,true);

        for ($i=0; $i < count($data['msg']); $i++) 
        { 
            $objActSheet->setCellValueExplicit('A' . $num,$data['msg'][$i]['jobNumber'] , PHPExcel_Cell_DataType::TYPE_STRING);           //工号
            $objActSheet->setCellValueExplicit('B' . $num,$data['msg'][$i]['name'] , PHPExcel_Cell_DataType::TYPE_STRING);                //姓名
            $objActSheet->setCellValueExplicit('C' . $num,$data['msg'][$i]['sex'] , PHPExcel_Cell_DataType::TYPE_STRING);                 //性别
            $objActSheet->setCellValueExplicit('D' . $num,$data['msg'][$i]['department1'] , PHPExcel_Cell_DataType::TYPE_STRING);         //一级部门名称
            $objActSheet->setCellValueExplicit('E' . $num,$data['msg'][$i]['department2'] , PHPExcel_Cell_DataType::TYPE_STRING);         //二级部门名称 
            $objActSheet->setCellValueExplicit('F' . $num,$data['msg'][$i]['department3'] , PHPExcel_Cell_DataType::TYPE_STRING);         //三级部门名称
            $objActSheet->setCellValueExplicit('G' . $num,$data['msg'][$i]['department4'], PHPExcel_Cell_DataType::TYPE_STRING);          //四级部门名称
            $objActSheet->setCellValueExplicit('H' . $num,$data['msg'][$i]['department5'], PHPExcel_Cell_DataType::TYPE_STRING);          //五级部门名称
            $objActSheet->setCellValueExplicit('I' . $num,$data['msg'][$i]['department6'], PHPExcel_Cell_DataType::TYPE_STRING);          //六级部门名称
            $objActSheet->setCellValueExplicit('J' . $num,$data['msg'][$i]['birthDate'], PHPExcel_Cell_DataType::TYPE_STRING);            //出生日期
            $objActSheet->setCellValueExplicit('K' . $num,$data['msg'][$i]['maritalStatus'], PHPExcel_Cell_DataType::TYPE_STRING);        //婚姻状况
            $objActSheet->setCellValueExplicit('L' . $num,$data['msg'][$i]['entryDate'], PHPExcel_Cell_DataType::TYPE_STRING);            //入职日期
            $objActSheet->setCellValueExplicit('M' . $num,$data['msg'][$i]['resignationDate'], PHPExcel_Cell_DataType::TYPE_STRING);      //离职日期
            $objActSheet->setCellValueExplicit('N' . $num,$data['msg'][$i]['lengthService'], PHPExcel_Cell_DataType::TYPE_STRING);        //工龄
            $objActSheet->setCellValueExplicit('O' . $num,$data['msg'][$i]['job'], PHPExcel_Cell_DataType::TYPE_STRING);                  //岗位
            $objActSheet->setCellValueExplicit('P' . $num,$data['msg'][$i]['phone'], PHPExcel_Cell_DataType::TYPE_STRING);                //联系方式
            $objActSheet->setCellValueExplicit('Q' . $num,$data['msg'][$i]['idNumber'], PHPExcel_Cell_DataType::TYPE_STRING);             //身份证号
            //设置行的属性  
            $objActSheet->getRowDimension($num)->setRowHeight('20px');//设置行高  
            $num = $num + 1;
        }
        
        //设置列的属性  
        $objActSheet->getColumnDimension("A")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("B")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("C")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("D")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("E")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("F")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("G")->setWidth(20); // 设置宽度
        $objActSheet->getColumnDimension("H")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("I")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("J")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("K")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("L")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("M")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("N")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("O")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("P")->setWidth(20); // 设置宽度 
        $objActSheet->getColumnDimension("Q")->setWidth(20); // 设置宽度 
        $outputFileName = iconv('UTF-8', 'gb2312', '人员花名册.xlsx');  

        //文件直接输出到浏览器  
        header('Pragma:public');  
        header('Expires:0');  
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');  
        header('Content-Type:application/force-download');  
        header('Content-Type:application/vnd.ms-excel');  
        header('Content-Type:application/octet-stream');  
        header('Content-Type:application/download');  
        header('Content-Disposition:attachment;filename=' . $outputFileName);  
        header('Content-Transfer-Encoding:binary');  
        PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007')->save('php://output');
                
        //设置对齐方式      
        $objStyleA3 = $objActSheet->getStyle('A3');  
        $objAlignA3 = $objStyleA3->getAlignment();  
        $objAlignA3->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);  
        $objAlignA3->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        
        $objExcel = null;

        //导出成功后清空数据库表
        $this->db->truncate('dorm_roster');
    }
}
?>